﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;

    public class Common
    {
        private readonly KeRui.DAL.Common dal = new KeRui.DAL.Common();

        public static void CreateSite(string strP, int isTruePath)
        {
            HttpServerUtility server = HttpContext.Current.Server;
            if (isTruePath == 0)
            {
                strP = server.MapPath(strP);
            }
            DirectoryInfo info = new DirectoryInfo(strP);
            if (!info.Exists)
            {
                info.Create();
            }
        }

        public string DatabaseBackup(string BakPath)
        {
            return this.dal.DatabaseBackup(BakPath);
        }

        public void DatabaseLogDel()
        {
            this.dal.DatabaseLogDel();
        }

        public string DatabaseName()
        {
            return this.dal.DatabaseName();
        }

        public DataSet DatabaseTable()
        {
            return this.dal.DatabaseTable();
        }

        public DataSet DatabaseTable(string tblName)
        {
            return this.dal.DatabaseTable(tblName);
        }

        public static string DelFile(string strP, int isTruePath)
        {
            HttpServerUtility server = HttpContext.Current.Server;
            if (isTruePath == 0)
            {
                strP = server.MapPath(strP);
            }
            if (System.IO.File.Exists(strP))
            {
                System.IO.File.Delete(strP);
            }
            return "";
        }

        public static void ExecuteSQL(string sql)
        {
            KeRui.DAL.Common.ExecuteSQL(sql);
        }

        public static string GetCheckBoxList(string chkName, string intTmp, DataSet ds)
        {
            DataTable table = ds.Tables[0];
            string str = "";
            intTmp = "," + intTmp + ",";
            if (table.Rows.Count == 0)
            {
                return (str + "当前没有站点");
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (intTmp.IndexOf("," + table.Rows[i][0].ToString() + ",") >= 0)
                {
                    str = str + "<li><input type=\"checkbox\" name=" + chkName + " value='" + table.Rows[i][0].ToString() + "' checked />" + table.Rows[i][1].ToString() + "</li>";
                }
                else
                {
                    str = str + "<li><input type=\"checkbox\" name=" + chkName + " value='" + table.Rows[i][0].ToString() + "' />" + table.Rows[i][1].ToString() + "</li>";
                }
            }
            return str;
        }

        public static string GetCheckBoxListForModule(string chkName, string intTmp, DataSet ds, bool isAdd)
        {
            DataTable table = ds.Tables[0];
            string str = "";
            intTmp = "," + intTmp + ",";
            if (table.Rows.Count == 0)
            {
                return (str + "当前没有应用模块");
            }
            string str2 = "";
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (table.Rows[i][2].ToString() == "0")
                {
                    str2 = "disabled";
                    if (isAdd && (table.Rows[i][3].ToString() == "1"))
                    {
                        str = str + "<input type=\"hidden\" name=\"" + chkName + "\" value=\"" + table.Rows[i][0].ToString() + "\" />";
                    }
                }
                else
                {
                    str2 = "";
                }
                if (isAdd && (table.Rows[i][3].ToString() == "1"))
                {
                    str = str + "<li><input type=\"checkbox\" name=" + chkName + " value='" + table.Rows[i][0].ToString() + "' checked " + str2 + "/>" + table.Rows[i][1].ToString() + "</li>";
                }
                else if (intTmp.IndexOf("," + table.Rows[i][0].ToString() + ",") >= 0)
                {
                    str = str + "<li><input type=\"checkbox\" name=\"" + chkName + "\" value=\"" + table.Rows[i][0].ToString() + "\" checked " + str2 + "/>" + table.Rows[i][1].ToString() + "</li>";
                }
                else
                {
                    str = str + "<li><input type=\"checkbox\" name=" + chkName + " value='" + table.Rows[i][0].ToString() + "'  " + str2 + "/>" + table.Rows[i][1].ToString() + "</li>";
                }
            }
            return str;
        }

        public static string GetCheckBoxListForRole(string chkName, string intTmp, DataSet ds, int userID)
        {
            DataTable table = ds.Tables[0];
            string str = "";
            intTmp = "," + intTmp + ",";
            if (table.Rows.Count == 0)
            {
                return (str + "当前没有角色分类");
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (intTmp.IndexOf("," + table.Rows[i][0].ToString() + ",") >= 0)
                {
                    str = string.Concat(new object[] { str, "<li><input type=\"checkbox\" name=", chkName, " value='", table.Rows[i][0].ToString(), "' checked onclick=\"getRoleChk(", userID, ")\"/>", table.Rows[i][1].ToString(), "</li>" });
                }
                else
                {
                    str = string.Concat(new object[] { str, "<li><input type=\"checkbox\" name=", chkName, " value='", table.Rows[i][0].ToString(), "' onclick=\"getRoleChk(", userID, ")\" />", table.Rows[i][1].ToString(), "</li>" });
                }
            }
            return str;
        }

        public static string GetCheckBoxListForSite(string chkName, string strTmp, DataSet ds)
        {
            DataTable table = ds.Tables[0];
            string str = "";
            strTmp = "," + strTmp + ",";
            if (table.Rows.Count == 0)
            {
                return (str + "当前没有站点");
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (strTmp.IndexOf(",site_" + table.Rows[i][0].ToString() + ",") >= 0)
                {
                    str = str + "<li><input type=\"checkbox\" name=" + chkName + " value='site_" + table.Rows[i][0].ToString() + "' checked />" + table.Rows[i][1].ToString() + "</li>";
                }
                else
                {
                    str = str + "<li><input type=\"checkbox\" name=" + chkName + " value='site_" + table.Rows[i][0].ToString() + "' />" + table.Rows[i][1].ToString() + "</li>";
                }
            }
            return str;
        }

        public static string GetCheckBoxListForSite(string chkName, string strTmp_disabled, string strTmp, DataSet ds)
        {
            DataTable table = ds.Tables[0];
            string str = "";
            if (table.Rows.Count == 0)
            {
                return (str + "当前没有站点");
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string str2 = "";
                if (strTmp_disabled.IndexOf(",site_" + table.Rows[i][0].ToString() + ",") != -1)
                {
                    str2 = " checked disabled";
                }
                else if (strTmp.IndexOf(",site_" + table.Rows[i][0].ToString() + ",") != -1)
                {
                    str2 = " checked ";
                }
                str = str + "<li><input type=\"checkbox\" name=" + chkName + " value='site_" + table.Rows[i][0].ToString() + "' " + str2 + " />" + table.Rows[i][1].ToString() + "</li>";
            }
            return str;
        }

        public int GetCount(string tblName, string strWhere)
        {
            return this.dal.GetCount(tblName, strWhere);
        }

        public int GetCountByCache(string tblName, string strWhere)
        {
            string cacheKey = "DB_Count-" + tblName + "-" + strWhere;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetCount(tblName, strWhere);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (int) cache;
        }

        public DataSet GetDataTableBySql(string sql)
        {
            return this.dal.GetDataTableBySql(sql);
        }

        public DataSet GetList(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder)
        {
            return this.dal.GetList(tblName, PageSize, PageIndex, strWhere, strOrder);
        }

        public DataSet GetList(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string fldName)
        {
            return this.dal.GetList(tblName, PageSize, PageIndex, strWhere, strOrder, fldName);
        }

        public DataSet GetList(string tblName, string strWhere, string strOrder, string fldName, int rowStart, int rowEnd)
        {
            return this.dal.GetList(tblName, strWhere, strOrder, fldName, rowStart, rowEnd);
        }

        public DataSet GetList(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string tblLink, string fldLink, string conLink)
        {
            return this.dal.GetList(tblName, PageSize, PageIndex, strWhere, strOrder, tblLink, fldLink, conLink);
        }

        public DataSet GetListByCache(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder)
        {
            string cacheKey = string.Concat(new object[] { "DB_List1-", tblName, "-", PageSize, "-", PageIndex, "-", strWhere, "-", strOrder });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(tblName, PageSize, PageIndex, strWhere, strOrder);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public DataSet GetListByCache(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string fldName)
        {
            string cacheKey = string.Concat(new object[] { "DB_List2-", tblName, "-", PageSize, "-", PageIndex, "-", strWhere, "-", strOrder, "-", fldName });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(tblName, PageSize, PageIndex, strWhere, strOrder, fldName);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public DataSet GetListByCache(string tblName, string strWhere, string strOrder, string fldName, int rowStart, int rowEnd)
        {
            string cacheKey = string.Concat(new object[] { "DB_List3-", tblName, "-", strWhere, "-", strOrder, "-", fldName, "-", rowStart, "-", rowEnd });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(tblName, strWhere, strOrder, fldName, rowStart, rowEnd);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public DataSet GetListByCache(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string tblLink, string fldLink, string conLink)
        {
            string cacheKey = string.Concat(new object[] { "DB_List4-", tblName, "-", PageSize, "-", PageIndex, "-", strWhere, "-", strOrder, "-", tblLink, "-", fldLink, "-", conLink });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(tblName, PageSize, PageIndex, strWhere, strOrder, tblLink, fldLink, conLink);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public DataSet GetListForMoreTable(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string fldName)
        {
            return this.dal.GetListForMoreTable(tblName, PageSize, PageIndex, strWhere, strOrder, fldName);
        }

        public DataSet GetListForMoreTableByCache(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string fldName)
        {
            string cacheKey = string.Concat(new object[] { "DB_ListForMoreTable-", tblName, "-", PageSize, "-", PageIndex, "-", strWhere, "-", strOrder, "-", fldName });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetListForMoreTable(tblName, PageSize, PageIndex, strWhere, strOrder, fldName);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public DataSet GetSelect(string tblName, string strWhere, string strOrder, string fldName)
        {
            return this.dal.GetSelect(tblName, strWhere, strOrder, fldName);
        }

        public static string GetSelectList(int intTmp, DataSet ds)
        {
            return GetSelectList(intTmp, ds.Tables[0], "");
        }

        public static string GetSelectList(int intTmp, DataTable dt)
        {
            return GetSelectList(intTmp, dt, "");
        }

        public static string GetSelectList(string strTmp, DataSet ds)
        {
            return GetSelectList(strTmp, ds.Tables[0], "");
        }

        public static string GetSelectList(string strTmp, DataTable dt)
        {
            return GetSelectList(strTmp, dt, "");
        }

        public static string GetSelectList(int intTmp, DataSet ds, string noSelected)
        {
            return GetSelectList(intTmp, ds.Tables[0], noSelected);
        }

        public static string GetSelectList(int intTmp, DataTable dt, string noSelected)
        {
            if (noSelected == "")
            {
                noSelected = "—请选择—";
            }
            string str = "";
            if (dt.Rows.Count == 0)
            {
                return (str + "<option value=''>当前没有选项</option>");
            }
            str = str + "<option value=''>" + noSelected + "</option>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToString(intTmp) == dt.Rows[i][0].ToString())
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' selected>" + dt.Rows[i][1].ToString() + "</option>";
                }
                else
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' >" + dt.Rows[i][1].ToString() + "</option>";
                }
            }
            return str;
        }

        public static string GetSelectList(string strTmp, DataSet ds, string noSelected)
        {
            return GetSelectList(strTmp, ds.Tables[0], noSelected);
        }

        public static string GetSelectList(string strTmp, DataTable dt, string noSelected)
        {
            if (noSelected == "")
            {
                noSelected = "—请选择—";
            }
            if (strTmp == null)
            {
                strTmp = "";
            }
            string str = "";
            if (dt.Rows.Count == 0)
            {
                return (str + "<option value=''>当前没有分类</option>");
            }
            str = str + "<option value=''>" + noSelected + "</option>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (strTmp.ToString().IndexOf(dt.Rows[i][0].ToString()) >= 0)
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' selected>" + dt.Rows[i][1].ToString() + "</option>";
                }
                else
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' >" + dt.Rows[i][1].ToString() + "</option>";
                }
            }
            return str;
        }

        public static string GetSelectListForAD(int strTmp, DataSet ds)
        {
            return GetSelectListForAD(strTmp, ds.Tables[0]);
        }

        public static string GetSelectListForAD(int strTmp, DataTable dt)
        {
            string str = "";
            if (dt.Rows.Count == 0)
            {
                return (str + "<option value=''>－－当前没有广告位－－</option>");
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (strTmp.ToString().IndexOf(dt.Rows[i][0].ToString()) >= 0)
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' selected>" + dt.Rows[i][1].ToString() + "</option>";
                }
                else
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' >" + dt.Rows[i][1].ToString() + "</option>";
                }
            }
            return str;
        }

        public static string GetSelectListForTemplate(string strTmp, DataSet ds)
        {
            return GetSelectListForTemplate(strTmp, ds.Tables[0]);
        }

        public static string GetSelectListForTemplate(string strTmp, DataTable dt)
        {
            string str = "";
            if (dt.Rows.Count == 0)
            {
                return (str + "<option value=''>－－当前没有模板－－</option>");
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (strTmp.ToString() == dt.Rows[i][0].ToString())
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' selected>" + dt.Rows[i][1].ToString() + "</option>";
                }
                else
                {
                    str = str + "<option value='" + dt.Rows[i][0].ToString() + "' >" + dt.Rows[i][1].ToString() + "</option>";
                }
            }
            return str;
        }

        public static string GetServerIP()
        {
            try
            {
                List<KeRui.Model.CmsServer> list = new KeRui.BLL.CmsServer().GetList(1);
                foreach (KeRui.Model.CmsServer server2 in list)
                {
                    string serverIP = GetServerIP(server2);
                    if (serverIP != "")
                    {
                        return serverIP;
                    }
                }
            }
            catch
            {
            }
            return "";
        }

        public static string GetServerIP(KeRui.Model.CmsServer model)
        {
            string ip = "";
            WebClient client = new WebClient();
            try
            {
                ip = client.DownloadString(model.ServerUrl + "/manage/service/ip/index.aspx?m=" + Utils.MD5(model.ServerKey));
                if ((ip != "") && Utils.IsIP(ip))
                {
                    client.Dispose();
                    return ip;
                }
            }
            catch
            {
            }
            client.Dispose();
            return "";
        }

        public static string LoadTxtFile(string strP, int isTruePath)
        {
            HttpServerUtility server = HttpContext.Current.Server;
            string str = "";
            if (isTruePath == 0)
            {
                strP = server.MapPath(strP);
            }
            if (System.IO.File.Exists(strP))
            {
                FileStream stream = new FileStream(strP, FileMode.Open, FileAccess.Read);
                StreamReader reader = new StreamReader(stream);
                str = reader.ReadToEnd();
                reader.Close();
                stream.Close();
            }
            return str;
        }

        public static void WriteShtmlFile(int isTruePath, string strP, string strContent)
        {
            HttpServerUtility server = HttpContext.Current.Server;
            if (isTruePath == 0)
            {
                strP = server.MapPath(strP);
            }
            StreamWriter writer = new StreamWriter(strP, false, Encoding.GetEncoding("UTF-8"));
            writer.Write(strContent);
            writer.Close();
            writer.Dispose();
        }
    }
}

